package com.windfone.web.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.common.Constant;
import com.windfone.common.OrderUtil;
import com.windfone.common.PageUtil;
import com.windfone.model.Pricehistory;
import com.windfone.model.Product;
import com.windfone.model.ProductType;
import com.windfone.service.CommonService;
import com.windfone.service.ProductExtendService;
import com.windfone.service.ProductService;
import com.windfone.service.ProductTypeService;

public class ProductAction  extends ActionSupport {
	/**	serialVersionUID */
	private static final long serialVersionUID = 1L;
	private ProductService productService;
	private ProductTypeService productTypeService;
	private ProductExtendService productExtendService;
	private CommonService commonService;
	/** page avaible area */
	private List<Product> productList;
	private List<ProductType> productTypeList;
	private int productTypeId;
	private int langId;
	private String typeName;

	private String name;
	private String marketTime;
	private String mobileFormat;
	private String phoneShape;
	private String screenSize;
	private String screenParam;
	private String system;
	private String gps;
	private String camera;
	private double price;
	private String priceStr;
	private String mobilePhase;
	private String screenObject;
	private String cpu;
	private String memory;
	private String standard;
	private String service;
	

	private String talkTime;
	private String shapeSize;
	private String shapeColor;
	private String weight;	
	private String waitTime;
	private String screenColor;
	
	private String imagepath;
	
	private String bigImagepath;
	
	private Product bean;
	private int beginRecord;
	private int currentPage;
	private int nextPage;
	private int totalPage;
	private int prevPage;
	private int productId;
	private int pointSeq;
	
	private int status;
	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getPointSeq() {
		return pointSeq;
	}

	public void setCommonService(CommonService commonService) {
		this.commonService = commonService;
	}

	public void setPointSeq(int pointSeq) {
		this.pointSeq = pointSeq;
	}

	public String getPriceStr() {
		return priceStr;
	}

	public void setProductExtendService(ProductExtendService productExtendService) {
		this.productExtendService = productExtendService;
	}

	public String getImagepath() {
		return imagepath;
	}

	public String getBigImagepath() {
		return bigImagepath;
	}

	public void setBigImagepath(String bigImagepath) {
		this.bigImagepath = bigImagepath;
	}

	public void setImagepath(String imagepath) {
		this.imagepath = imagepath;
	}

	public void setProductTypeService(ProductTypeService productTypeService) {
		this.productTypeService = productTypeService;
	}

	public void setPriceStr(String priceStr) {
		this.priceStr = priceStr;
	}
	
	public int getProductId() {
		return productId;
	}

	public String getService() {
		return service;
	}

	public void setService(String service) {
		this.service = service;
	}

	public List<ProductType> getProductTypeList() {
		return productTypeList;
	}

	public void setProductTypeList(List<ProductType> productTypeList) {
		this.productTypeList = productTypeList;
	}

	public void setProductId(int productId) {
		this.productId = productId;
	}

	public String getCpu() {
		return cpu;
	}

	public String getScreenObject() {
		return screenObject;
	}

	public void setScreenObject(String screenObject) {
		this.screenObject = screenObject;
	}

	public String getMobilePhase() {
		return mobilePhase;
	}

	public void setMobilePhase(String mobilePhase) {
		this.mobilePhase = mobilePhase;
	}

	public void setCpu(String cpu) {
		this.cpu = cpu;
	}

	public String getMemory() {
		return memory;
	}

	public String getWaitTime() {
		return waitTime;
	}

	public void setWaitTime(String waitTime) {
		this.waitTime = waitTime;
	}

	public String getScreenColor() {
		return screenColor;
	}

	public void setScreenColor(String screenColor) {
		this.screenColor = screenColor;
	}

	public void setMemory(String memory) {
		this.memory = memory;
	}

	public String getStandard() {
		return standard;
	}

	public void setStandard(String standard) {
		this.standard = standard;
	}

	public String getTalkTime() {
		return talkTime;
	}

	public void setTalkTime(String talkTime) {
		this.talkTime = talkTime;
	}

	public String getShapeSize() {
		return shapeSize;
	}

	public void setShapeSize(String shapeSize) {
		this.shapeSize = shapeSize;
	}

	public String getShapeColor() {
		return shapeColor;
	}

	public void setShapeColor(String shapeColor) {
		this.shapeColor = shapeColor;
	}

	public String getWeight() {
		return weight;
	}

	public void setWeight(String weight) {
		this.weight = weight;
	}

	public ProductService getProductService() {
		return productService;
	}

	public Product getBean() {
		return bean;
	}

	public int getLangId() {
		return langId;
	}

	public void setLangId(int langId) {
		this.langId = langId;
	}

	public int getProductTypeId() {
		return productTypeId;
	}

	public void setProductTypeId(int productTypeId) {
		this.productTypeId = productTypeId;
	}

	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	public void setBean(Product bean) {
		this.bean = bean;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPrevPage() {
		return prevPage;
	}

	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public List<Product> getProductList() {
		return productList;
	}

	public void setProductList(List<Product> productList) {
		this.productList = productList;
	}

	public int getBeginRecord() {
		return beginRecord;
	}

	public void setBeginRecord(int beginRecord) {
		this.beginRecord = beginRecord;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getMarketTime() {
		return marketTime;
	}

	public void setMarketTime(String marketTime) {
		this.marketTime = marketTime;
	}

	public String getMobileFormat() {
		return mobileFormat;
	}

	public void setMobileFormat(String mobileFormat) {
		this.mobileFormat = mobileFormat;
	}

	public String getPhoneShape() {
		return phoneShape;
	}

	public void setPhoneShape(String phoneShape) {
		this.phoneShape = phoneShape;
	}

	public String getScreenSize() {
		return screenSize;
	}

	public void setScreenSize(String screenSize) {
		this.screenSize = screenSize;
	}

	public String getScreenParam() {
		return screenParam;
	}

	public void setScreenParam(String screenParam) {
		this.screenParam = screenParam;
	}

	public String getSystem() {
		return system;
	}

	public void setSystem(String system) {
		this.system = system;
	}

	public String getGps() {
		return gps;
	}

	public void setGps(String gps) {
		this.gps = gps;
	}

	public String getCamera() {
		return camera;
	}

	public void setCamera(String camera) {
		this.camera = camera;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	public String addProduct()throws Exception {
		Product bean = new Product();
		bean.setLangId(langId);
		bean.setPointSeq(pointSeq);
		bean.setName(name);
		bean.setType(typeName);
		bean.setService(service);
//		productTypeId= 1;
		bean.setProductTypeId(productTypeId);
		bean.setMarketTime(marketTime );
		bean.setMobileFormat(mobileFormat);
		bean.setScreenObject(screenObject);
		bean.setPhoneShape( phoneShape);
		bean.setScreenSize( screenSize );
		bean.setScreenParam(screenParam);
		bean.setSystem(system);
		bean.setGps(gps);
		bean.setCamera(camera);
		bean.setMobilePhase(mobilePhase);
		if ( priceStr !=null && !priceStr.equals(""))
			price = Double.parseDouble(priceStr);
		bean.setPrice(price);
		bean.setCpu( cpu);
		bean.setMemory (memory);
		bean.setStandard( standard);
		bean.setTalkTime( talkTime);
		bean.setShapeSize( shapeSize);
		bean.setShapeColor( shapeColor);
		bean.setWeight( weight);						
		bean.setWaitTime( waitTime);						
		bean.setScreenColor( screenColor);			
		bean.setImagepath(imagepath);
		bean.setBigImagepath(bigImagepath);
		if(productService.insertProduct(bean) == 1){
			Pricehistory pricehistory =new Pricehistory();
			pricehistory.setDescription("");
			pricehistory.setStandar( bean.getStandard() );
			int keygroup=OrderUtil.getDateInt();
			pricehistory.setKeygroup(keygroup);
			pricehistory.setPrice( bean.getPrice() );
			pricehistory.setOldprice( bean.getPrice() );
			pricehistory.setPackageId(0);
			pricehistory.setProductId(productId);
			commonService.addPricehistory(pricehistory );
		}
		return SUCCESS;
	}
	
	public String updateMobileStatus() throws Exception {
		productService.updateProductStatus(productId, status);
		return SUCCESS;
	}
	public String deleteProduct()throws Exception {
		productExtendService.deleteProductBaseByProductId(productId);
		productExtendService.deleteProductCameraFuncByProductId(productId);
		productExtendService.deleteProductDigiFuncByProductId(productId);
		productExtendService.deleteProductMultimediaFuncByProductId(productId);
		productService.deleteProduct(productId);
		return SUCCESS;
	}
	
	public String beforUpdateProduct()throws Exception {
		bean = productService.getProductById(productId).get(0);
		productTypeList = productTypeService.listProductType();
		return SUCCESS;
	}
	
	public String updateProduct()throws Exception {
		Product bean = new Product();
		bean.setLangId(langId);
		bean.setName(name);
		bean.setPointSeq(pointSeq);
		bean.setType(typeName);
		bean.setService(service);
		bean.setProductTypeId(productTypeId);
		bean.setMarketTime(marketTime );
		bean.setMobileFormat(mobileFormat);
		bean.setPhoneShape( phoneShape);
		bean.setScreenSize( screenSize );
		bean.setSystem(system);
		bean.setMobilePhase(mobilePhase);
		bean.setScreenObject(screenObject);
		bean.setScreenParam(screenParam);
		bean.setGps(gps);
		bean.setCamera(camera);
		if ( priceStr !=null && !priceStr.equals(""))
			price = Double.parseDouble(priceStr);		
		bean.setPrice(price);
		bean.setCpu( cpu);
		bean.setMemory (memory);
		bean.setStandard( standard);
		bean.setTalkTime( talkTime);
		bean.setShapeSize( shapeSize);
		bean.setShapeColor( shapeColor);
		bean.setWeight( weight);						
		bean.setWaitTime( waitTime);						
		bean.setScreenColor( screenColor);
		bean.setId(productId);
		bean.setImagepath(imagepath);
		bean.setBigImagepath(bigImagepath);
		if (productService.updateProduct(bean)==1){
			Pricehistory pricehistory =new Pricehistory();
			pricehistory.setDescription("");
			pricehistory.setStandar( bean.getStandard() );
			int keygroup=OrderUtil.getDateInt();
			pricehistory.setKeygroup(keygroup);
			pricehistory.setPrice( bean.getPrice() );
			pricehistory.setOldprice( bean.getPrice() );
			pricehistory.setPackageId(0);
			pricehistory.setProductId(productId);
			Pricehistory tmp = commonService.checkPriceExist(pricehistory);
			if ( tmp == null){
				commonService.addPricehistory( pricehistory );
			} else {
				pricehistory.setId(tmp.getId());
				pricehistory.setRange( (bean.getPrice()- tmp.getOldprice()) );
				commonService.updatePricehistory( pricehistory );
			}
		}
		return SUCCESS;
	}
	
	private String beginDate;
	private String endDate;
	private int productyTypeIdQ = -1;
	public String productList() throws Exception{
		productTypeList = productTypeService.listProductType();
		String whereCondition = "";
		if ( productyTypeIdQ !=-1 ){
			whereCondition = " AND productTypeId="+ productyTypeIdQ;
		}
		if ( beginDate!=null && !beginDate.equals("") ){
			whereCondition = whereCondition + "  AND p.createTime>='"+beginDate +"' ";
		}
		if ( endDate!=null && !endDate.equals("") ){
			whereCondition = whereCondition + "  AND p.createTime<='"+ endDate  +"'  ";
		}
		PageUtil pu = new PageUtil();
		pu.setCurrentPage(currentPage);
		int totalRecords = productService.getAllRecorderNumber(whereCondition) ;
		pu.setTotalRecords( totalRecords );
		pu.setPageSize(Constant.PAGE_SIZE);
		pu.dealPage();
		currentPage = pu.getCurrentPage();
		nextPage = pu.getNextPage();
		totalPage = pu.getTotalPage();
		prevPage = pu.getPrevPage();
		
		beginRecord = Constant.PAGE_SIZE * (currentPage -1);
		if ( beginRecord< 0 )
			beginRecord = 0; 
		productList = productService.getAllProduct(beginRecord,whereCondition);
		return SUCCESS;
	}

	public String getBeginDate() {
		return beginDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public int getProductyTypeIdQ() {
		return productyTypeIdQ;
	}

	public void setProductyTypeIdQ(int productyTypeIdQ) {
		this.productyTypeIdQ = productyTypeIdQ;
	}

	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
}
